package io.aicn.lowcode.generator.independent.util;

import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Component
public class FileWriteUtil {

    public void writeFile(String filePath, String content) throws IOException {
        Path path = Paths.get(filePath);
        
        // 创建父目录
        Files.createDirectories(path.getParent());
        
        // 写入文件
        Files.write(path, content.getBytes());
    }

    public boolean fileExists(String filePath) {
        return Files.exists(Paths.get(filePath));
    }

    public void createDirectory(String dirPath) throws IOException {
        Files.createDirectories(Paths.get(dirPath));
    }

    public String getRelativePath(String basePath, String filePath) {
        return Paths.get(basePath).relativize(Paths.get(filePath)).toString();
    }
}